
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<div class="layui-fluid layui-anim febs-anim" id="activity" lay-title="活动列表">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="activity-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">活动名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" placeholder="请输入活动名称" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">活动地点</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="position" placeholder="请输入活动地点" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>搜索
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>刷新
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="activity:add,activity:update,activity:delete">
                                    <i class="layui-icon">&#xe875;</i>更多
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="activity_table" id="activeListTable" class="layui-table" lay-data="{id: 'activityTable',skin:'line', even:true, size:'lg'}"></table>
                    <style type="text/css" >
                         .layui-table-cell{
                            height:36px;
                            line-height: 36px;
                        }
                    </style>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="user-status">
    {{#
    var state = {
    1: {title: '有效', color: 'green'},
    0: {title: '禁用', color: 'volcano'}
    }[d.state];
    }}
    <span class="layui-badge febs-tag-{{state.color}}">{{ state.title }}</span>
</script>
<script type="text/html" id="activity-option">
    <span shiro:lacksPermission="activity:add,activity:update,activity:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="activity:view"><i
            class="layui-icon febs-edit-area febs-green">&#xe7a5;</i><span class="febs-green">查看详情</span></a>

    <a lay-event="activityEdit" shiro:hasPermission="activity:update"><i
            class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i>设置</a>
    <a lay-event="del" shiro:hasPermission="activity:delete"><i
            class="layui-icon febs-edit-area febs-red">&#xe7f9;</i><span class="febs-red">删除</span></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#activity'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;

        form.render();
        initTable();

        laydate.render({
            elem: '#user-createTime',
            range: true,
            trigger: 'click'
        });



        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('activity_table');
                if (name === 'add') {
                    febs.modal.open('新增活动', 'system/activity/add', {
                        btn: ['提交', '重置'],
                        area: ['50%', '70%'],
                        yes: function (index, layero) {
                            $('#activityAdd').find('#submit').trigger('click');
                        },
                        btn2: function () {
                            $('#activityAdd').find('#reset').trigger('click');
                            return false;
                        }
                        // end:function () {
                        //     $reset.click();
                        // }
                    });
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的活动');
                    } else {
                        febs.modal.confirm('删除活动', '确定删除该活动？', function () {
                            var tActivityIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                tActivityIds.push(item.tactivityId)
                            });
                            deleteUsers(tActivityIds.join(','));
                        });
                    }
                }


            },
            options: [{
                name: 'add',
                title: '新增活动',
                perms: 'activity:add'
            }, {
                name: 'delete',
                title: '删除活动',
                perms: 'activity:delete'
            }]
        });


        table.on('tool(activity_table)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;

            if (layEvent === 'detail') {
                febs.modal.view('活动信息', 'system/activity/activityView/' + data.tactivityId, {
                    // area: $(window).width() <= 750 ? '80%' : '660px'
                    area:['700px','800px']
                });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除活动', '确定删除该活动？', function () {
                    deleteUsers(data.tactivityId);
                });
            }
            if (layEvent === 'activityEdit') {
                febs.modal.open('修改活动', 'system/activity/update/' + data.tactivityId, {
                    area: ['50%', '70%'],
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#activity-update-form').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
            if (layEvent === 'contentEdit') {
                febs.modal.open('修改活动内容', 'system/activity/updateContent/' + data.tactivityId, {
                    area: ['50%', '70%'],
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#content-update-form').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
            if (layEvent === 'report') {
                window.location.href = ctx+"activity/downloadFile?objectName="+data.servicePhoto;
            }
            // if (layEvent === 'projectContentUrl') {
            //     window.location.href = ctx+"activity/downloadFile?objectName="+data.projectContentUrl;
            // }
            if (layEvent === 'toSeeDetails') {
                var index = layer.open({
                    title: "查看图片详情",
                    content: data.headPhoto,
                    type: 2,
                    maxmin: true,
                    area: ['60%','60%'],
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        // console.log(body.html()); //得到iframe页的body内容
                        // body.find('#goods_id').val(ids);
                    }
                });
            }

            if (layEvent === 'toSeeWechat') {
                var index = layer.open({
                    title: "查看微信二维码",
                    content: data.servicePhoto,
                    type: 2,
                    maxmin: true,
                    area: ['60%','60%'],
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        // console.log(body.html()); //得到iframe页的body内容
                        // body.find('#goods_id').val(ids);
                    }
                });
            }

            if (layEvent === 'projectContent'){
                var index = layer.open({
                    title: "编辑实验",
                    content: ctx+'febs/views/system/editProjectContent',
                    type: 2,
                    maxmin: true,
                    area: ['50%','50%'],
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        // console.log(body.html()); //得到iframe页的body内容
                        // body.find('#goods_id').val(ids);
                    }
                });
            }

            if (layEvent === 'show_content'){
                $.ajax({
                    url:ctx+'activity/getContent',
                    data: {tActivityId: data.tactivityId},
                    type: 'get',
                    dataType: 'json',
                    success: function (e) {
                        layer.open({
                            title: "查看活动内容",
                            content: e.data,
                            type: 1,
                            maxmin: true,
                            area: ['50%','50%']
                        });
                    }
                })
            }

        });

        table.on('sort(activity_table)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params});
        });


        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(),  initSort: sortObject,
                done: function(res, curr, count){
                    //如果是异步请求数据方式，res即为你接口返回的信息。
                    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                    //得到当前页码
                    //得到数据总量
                    var beforeCurr = curr;
                    //当前页数据的个数
                    var dataLength = res.data.length;
                    var count = res.count;
                    //如果当前页数据的个数等于0，并且总数不为0，则跳转到上一页
                    if( dataLength == 0 && count!= 0 ){
                        table.reload("activity_table",{
                            page:{curr:beforeCurr - 1}
                        })
                    }
                }
            });
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'activity_table',
                // height: 'full-20',
                url: ctx + 'activity/getActivityList',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'name', title: '活动名称',minWidth: 300},
                    // {field: 'position', title: '活动地点',minWidth: 100},
                    {
                        field: 'headPhoto', align:'center',title: '活动头图',minWidth: 100,rowspan: 2, templet: function (d) {
                            if (d.headPhoto!=null) {
                                return '<img style="width: 200px;height:160px" lay-event="toSeeDetails" src="' + d.headPhoto + '">'
                            }else {
                                return '';
                            }
                        }
                    },

                    {field: 'price', title: '价格',minWidth: 50},
                    // {field: 'distribution', title: '分销比',minWidth: 50 },
                    {field: 'limitNumber', title: '限额人数'},
                    {field: 'visitNum', title: '浏览量',minWidth: 50},
                    { title: '活动内容',minWidth: 100,templet: function (d) {
                            return ' <a lay-event="contentEdit" ><i class="layui-icon febs-edit-area febs-purple">&#xe63c;</i>内容详情</a>';
                        }},
                    {field: 'serviceTel', title: '客服手机号', minWidth:120},
                    // {
                    //     field: 'servicePhoto',align:'center', title: '客服微信', templet: function (d) {
                    //         if (d.servicePhoto!=null) {
                    //             return '<img style="width: 200px;height:160px" lay-event="toSeeWechat" src="' + d.servicePhoto + '">'
                    //         }else {
                    //             return '';
                    //         }
                    //     }
                    // },
                    // {
                    //     field: 'videoUrl', title: '背景音乐', templet: function (d) {
                    //         if (d.videoUrl!=null) {
                    //             var ss = d.videoUrl.split('/');
                    //             var dd = ss[ss.length - 1];
                    //             return dd
                    //         }else {
                    //             return '';
                    //         }
                    //     }
                    // },
                    {field: 'endTime', title: '活动时间', minWidth: 150,templet:function (d) {
                            if (d.endTime != null){
                                return layui.util.toDateString(d.endTime, 'yyyy年MM月dd日')
                            }else{
                                return ''
                            }
                        }},
                    {field:'isPublish',align:'center',title: '发布',templet: function (data) {
                            if (data.isPublish == 1) {
                                return '<div> <input type="checkbox" checked="" swicthName='+data.isPublish+'  name="isPublish" lay-skin="switch" id="open"  lay-filter="switchTest" switchId=' + data.tactivityId + '' +
                                    ' lay-text="发布|未发布"  value=' + data.isPublish + '></div>';
                            }else{
                                return '<div> <input type="checkbox" lay-skin="switch" name="isPublish"  switchId=' + data.tactivityId + ' lay-filter="switchTest"' +
                                    'lay-text="发布|未发布" value=' + data.isPublish + '></div>';
                            }
                        }},
                    {title: '操作', toolbar: '#activity-option',align:"center", minWidth: 250}
                ]]
            });
        }

        /**
         * 监听开关 状态 操作
         */
        form.on('switch(switchTest)', function (data) {
            /**
             * 禁用标签
             * 状态 赋值为 1
             */
            var layerIndex = layer.load(3);

            if ((this.checked ? 'true' : 'false') == 'false') {
                $.ajax({
                    url: '/activity/updatePublish',
                    data: {
                        isPublish: 0,
                        tActivityId: data.elem.getAttribute("switchId"),
                    },
                    type: 'POST', //HTTP请求类型
                    success: function (data) {
                        febs.alert.success('取消活动发布成功');
                        $('#febs-share').find('#reset').click();
                    }, error: function () {
                        febs.alert.fail('取消活动发布成功失败');
                    }
                })
            } else {
                /**
                 * 启动标签
                 * 状态 赋值为 0
                 */
                $.ajax({
                    url: 'activity/updatePublish',
                    data: {
                        isPublish: 1,
                        tActivityId: data.elem.getAttribute("switchId"),
                    },
                    type: 'POST',
                    success: function (data) {
                        febs.alert.success('发布活动成功');
                        $('#febs-share').find('#reset').click();
                    }, error: function () {
                        febs.alert.fail('发布活动失败');
                    }
                })
            }
            layer.close(layerIndex);

        });


        function getQueryParams() {
            return {
                name: $searchForm.find('input[name="name"]').val().trim(),
                position: $searchForm.find('input[name="position"]').val().trim(),
                invalidate_ie_cache: new Date()
            };
        }

        function deleteUsers(tActivityIds) {
            febs.get(ctx + 'activity/delete/' + tActivityIds, null, function () {
                febs.alert.success('删除活动成功');
                $query.click();
            });
        }
    })
</script>